package com.ctg.behavior.calc.impala.builder.userbehavior;

import java.util.ArrayList;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;

import org.springframework.stereotype.Component;

import com.ctg.behavior.calc.impala.report.userbehavior.CtUBReport;
import com.ctg.behavior.common.consts.CtCalcConsts;
import com.ctg.behavior.common.consts.CtFieldConsts;
import com.ctg.behavior.common.userbehavior.CtUBCalcData;
import com.ctg.behavior.common.userbehavior.CtUBCalcRowData;
import com.ctg.behavior.utils.CtDateUtils;

@Component
public class CtUBCalcResultBuilder {

	public CtUBCalcData doBuilder(CtUBCalcData calcData, List<LinkedHashMap<String, Object>> calcResult, final CtUBReport report) {

		if (CtCalcConsts.LOAD_TYPE_EVENT_TOTAL.equalsIgnoreCase(report.getLoadType())) {
			// 事件总量统计 - 按时间维度统计
			return this.doEventTotalBuilder(calcData, calcResult, report);
		} else if (CtCalcConsts.LOAD_TYPE_EVENT_GROUPBY.equalsIgnoreCase(report.getLoadType())) {
			// 事件分组统计 - 按事件分组维度统计
			return this.doEventGroupByBuilder(calcData, calcResult, report);
		}

		return calcData;
	}

	public CtUBCalcData doEventTotalBuilder(CtUBCalcData calcData, List<LinkedHashMap<String, Object>> calcResult, final CtUBReport report) {

		// 实际计算结果
		Long total = 0L;
		if (!calcResult.isEmpty()) {
			total = Long.valueOf(calcResult.get(0).get(CtFieldConsts.FIELD_KEY_TOTAL).toString());
		}
		// y轴值：实际统计结果值
		CtUBCalcRowData rowData = new CtUBCalcRowData();
		calcData.getRows().add(rowData);
		List<Double> yValues = new ArrayList<>();
		yValues.add(total.doubleValue());
		rowData.getValues().add(yValues);
		// 事件总量统计 - 按时间维度统计
		// series系列：x坐标
		calcData.getSeries().add(CtDateUtils.dateToString(report.getCalcDate().getFromDate()));
		// 行数累加
		calcData.setTotalRows(calcData.getTotalRows() + 1);
		calcData.setNumRows(calcData.getNumRows() + 1);
		// 基本请求参数
		if (0 == report.getReportIndex()) {
			calcData.setRequestId(report.getRequestId());
			calcData.setAppId(report.getAppId());
			calcData.setBookmarkId(report.getBookmarkId());

			calcData.setDataUpdateTime(CtDateUtils.dateToString(new Date()));
			calcData.setReportUpdateTime(CtDateUtils.dateToString(new Date()));
		}

		return calcData;
	}

	public CtUBCalcData doEventGroupByBuilder(CtUBCalcData calcData, List<LinkedHashMap<String, Object>> calcResult, final CtUBReport report) {

		// 只取第一个report中的值
		if (0 == report.getReportIndex()) {
			// byFields
			calcData.setByFields(report.getByFields());
			// series
			calcData.getSeries().add(CtDateUtils.dateToString(report.getCalcDate().getFromDate()));
		}
		// 按事件分组的计算结果
		for (LinkedHashMap<String, Object> oneRow : calcResult) {
			// 每一个事件的统计结果值
			Long total = 0L;
			if (!calcResult.isEmpty()) {
				total = Long.valueOf(oneRow.get(CtFieldConsts.FIELD_KEY_TOTAL).toString());
			}
			CtUBCalcRowData rowData = new CtUBCalcRowData();
			calcData.getRows().add(rowData);
			List<Double> yValues = new ArrayList<>();
			yValues.add(total.doubleValue());
			rowData.getValues().add(yValues);
			// 分组事件
			String event = oneRow.get(CtFieldConsts.FIELD_KEY_EVENT).toString();
			rowData.getByValues().add(event);
			// 行数累加
			calcData.setTotalRows(calcData.getTotalRows() + 1);
			calcData.setNumRows(calcData.getNumRows() + 1);
		}
		// 基本请求参数
		if (0 == report.getReportIndex()) {
			calcData.setRequestId(report.getRequestId());
			calcData.setAppId(report.getAppId());
			calcData.setBookmarkId(report.getBookmarkId());

			calcData.setDataUpdateTime(CtDateUtils.dateToString(new Date()));
			calcData.setReportUpdateTime(CtDateUtils.dateToString(new Date()));
		}

		return calcData;
	}

}
